package com.taotao.cart.service.impl;

import com.taotao.cart.jedis.JedisClient;
import com.taotao.cart.service.CartService;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.util.JsonUtils;
import com.taotao.manager.pojo.TbItem;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private JedisClient jedis;
    @Value("${TT_CART_REDIS_PRE_KEY}")
    private String TT_CART_REDIS_PRE_KEY;

    //登录状态下添加购物车
    @Override
    public TaotaoResult addItemCart(Long userId,TbItem item, Integer num) {
        String key=TT_CART_REDIS_PRE_KEY+":"+userId;
        String field = item.getId() + "";
        //根据key和field获取某一个商品
        TbItem tbItem = queryItemByUserIdAndItemId(userId, item.getId());
        //判断要添加的商品是否已存在于购物车列表中
        if (tbItem != null) {
            //如果已经存在,数量直接相加即可,然后再回写到redis中
            tbItem.setNum(tbItem.getNum()+num);
            jedis.hset(key, field, JsonUtils.objectToJson(tbItem));
        }else{
            //如果不存在,需要把商品信息添加到redis中
            item.setNum(num);
            if (item.getImage() != null) {
                item.setImage(item.getImage().split(",")[0]);
            }
            jedis.hset(key, field, JsonUtils.objectToJson(item));
        }
        return TaotaoResult.ok();
    }

    @Override
    public List<TbItem> getCartList(Long userId) {
        String key=TT_CART_REDIS_PRE_KEY+":"+userId;
        Map<String, String> map = jedis.hgetAll(key);
        List<TbItem> list = new ArrayList<>();
        if (map != null) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                String value = entry.getValue();
                TbItem tbItem = JsonUtils.jsonToPojo(value, TbItem.class);
                list.add(tbItem);
            }
        }
        return list;
    }

    @Override
    public TaotaoResult updateItemCartByItemId(Long userId, Long itemId, Integer num) {
        String key=TT_CART_REDIS_PRE_KEY+":"+userId;
        String field = itemId + "";
        TbItem tbItem = queryItemByUserIdAndItemId(userId, itemId);
        if (tbItem != null) {
            tbItem.setNum(num);
            jedis.hset(key, field, JsonUtils.objectToJson(tbItem));
        }else {
//            addItemCart(userId,tbItem,num);
        }
        return TaotaoResult.ok();
    }

    @Override
    public TaotaoResult deleteItemCartByItemId(Long userId, Long itemId) {
        String key=TT_CART_REDIS_PRE_KEY+":"+userId;
        String field = itemId + "";
        jedis.hdel(key, field);
        return TaotaoResult.ok();
    }

    /**
     * 查询用户下商品id为itemId的商品
     * @param userId
     * @param itemId
     * @return
     */
    private TbItem queryItemByUserIdAndItemId(Long userId, Long itemId) {
        String key=TT_CART_REDIS_PRE_KEY+":"+userId;
        String field = itemId + "";
        String s = jedis.hget(key, field);
        if (StringUtils.isNotBlank(s)) {
            TbItem tbItem = JsonUtils.jsonToPojo(s, TbItem.class);
            return tbItem;
        }
        return null;
    }
}
